package 剑指offer;

import common.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangjunfei
 * @date 2023/7/18 15:39
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 *
 *
 */
public class O_06 {

    public int[] reversePrint(ListNode head) {
        // if (head == null) {
        //     return new int[0];
        // }
        // List<Integer> list = new ArrayList<>();
        //
        // list.add(head.val);
        // while (head.next != null) {
        //     head = head.next;
        //     list.add(0, head.val);
        // }
        // int[] result = new int[list.size()];
        // for (int i = 0; i < list.size(); i++) {
        //     result[i] = list.get(i);
        // }
        // return result;

        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }

        int[] result = new int[count];

        cur = head;
        count--;
        while (cur != null && count >= 0) {
            result[count--] = cur.val;
            cur = cur.next;
        }
        return result;



    }
}
